﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CL_Helpers
{
    public class CL_Json
    {

        /// <summary>
        /// 从本地读取json文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="ErrText"></param>
        /// <returns></returns>
        public static string LoadJsonFromFile(string filePath, out string ErrText)
        {
            string jsonStr = string.Empty;
            ErrText = "";
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (StreamReader streamReader = new StreamReader(fs, Encoding.UTF8))
                    {
                        jsonStr = streamReader.ReadToEnd();
                    }
                }
                return jsonStr;
            }
            catch (Exception ex)
            {
                ErrText = ex.Message;
                return jsonStr;
            }
        }

        /// <summary>
        /// 保存json文件到本地
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="jsonStr"></param>
        /// <param name="ErrText"></param>
        /// <returns></returns>
        public static bool SaveJsonToFile(string filePath, string jsonStr, out string ErrText)
        {
            ErrText = "";
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                {
                    using (StreamWriter streamWriter = new StreamWriter(fs))
                    {
                        streamWriter.Write(jsonStr);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrText = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 实体对象转换成JSON字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="x"></param>
        /// <returns></returns>
        public static string EntityToJSON<T>(T x)
        {
            string result = string.Empty;

            try
            {
                result = JsonConvert.SerializeObject(x);
            }
            catch (Exception)
            {
                result = string.Empty;
            }
            return result;

        }

        /// <summary>
        /// JSON字符串转换成实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T JSONToEntity<T>(string jsonStr)
        {
            T t = default(T);
            try
            {
                t = (T)JsonConvert.DeserializeObject(jsonStr, typeof(T));
            }
            catch (Exception)
            {
                t = default(T);
            }

            return t;
        }

        /// <summary>
        /// 解析为 Dictionary
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <param name="ErrText"></param>
        /// <returns></returns>
        public static Dictionary<string, object> ParseDictionary(string jsonStr, out string ErrText)
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            ErrText = string.Empty;
            try
            {
                dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
            }
            catch (Exception ex)
            {
                ErrText = ex.Message;
            }
            return dict;
        }

        public static JArray ParseJArray(string jsonStr, out string ErrText)
        {
            JArray ja = new JArray();
            ErrText = string.Empty;
            try
            {
                ja = JsonConvert.DeserializeObject<JArray>(jsonStr);
            }
            catch (Exception ex)
            {
                ErrText = ex.Message;
            }
            return ja;
        }

        public static T ParseT<T>(string jsonStr, out string ErrText)
        {
            T t = default(T);
            ErrText = string.Empty;
            try
            {
                t = JsonConvert.DeserializeObject<T>(jsonStr);
            }
            catch (Exception ex)
            {
                ErrText = ex.Message;
            }
            return t;
        }

        /// <summary>
        /// 解析一维数组为List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonStr"></param>
        /// <param name="ErrText"></param>
        /// <returns></returns>
        public static List<T> ParseList<T>(string jsonStr, out string ErrText)
        {
            List<T> ts = new List<T>();
            ErrText = string.Empty;
            try
            {
                ts = JsonConvert.DeserializeObject<List<T>>(jsonStr);
            }
            catch (Exception ex)
            {
                ErrText = ex.Message;
            }
            return ts;
        }

    }
}
